package com.mytest.example.gobrs.config;

import com.gobrs.async.threadpool.GobrsAsyncThreadPoolFactory;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class GobrsAsyncConfig {
    private static final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("GobrsAsync-worker-%d").build();

    //默认使用的是 Executors.newCachedThreadPool() 的线程池
    @Autowired
    private GobrsAsyncThreadPoolFactory factory;

    @PostConstruct
    public void gobrsThreadPoolExecutor() {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(300, 500, 30, TimeUnit.SECONDS,
                new LinkedBlockingQueue(), threadFactory);
        factory.setThreadPoolExecutor(threadPoolExecutor);

    }
}
